package com.gdth.bank.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.bank.adapter.MenuListAdapter;
import com.gdth.model.ImageItem;
import com.gdth.model.Item;
import com.gdth.widget.dialog.BaseDialog;

public class DialogList extends BaseDialog{
	private TextView txt_title;
	/**
	 * 下拉列表
	 */
	private ListView listView;
	/**
	 * 数据源
	 */
	public List<ImageItem> items;
	
	/**
	 * 
	 */
	MenuListAdapter mMenuListAdapter;
	
	OnItemClickListener mOnItemClickListener;
	
	public DialogList(Context context, String title, boolean dismiss) {
		super(context, R.style.dialog_theme);
	
		this.title = title;
		this.dismiss = dismiss;
		
		initView();
	}
	
	public DialogList(Context context, String title, boolean dismiss, OnItemClickListener mOnItemClickListener) {
		super(context, R.style.dialog_theme);
	
		this.title = title;
		this.dismiss = dismiss;
		this.mOnItemClickListener = mOnItemClickListener;
		
		initView();
	}
	
	protected void initView(){
		super.initView(R.layout.widget_menu_list);
		
		LinearLayout linearlayout = (LinearLayout) findViewById(R.id.dialog_content);
		
		if (dismiss)
			linearlayout.setOnClickListener(new dismissOnClickListener());
        
		items = new ArrayList<ImageItem>();
		
		mMenuListAdapter = new MenuListAdapter(this.getContext(), this.items);  

		txt_title = (TextView) this.findViewById(R.id.dialog_title);
		txt_title.setText(title);
		listView = (ListView) this.findViewById(R.id.dialog_listview); 
		listView.setBackgroundResource(R.drawable.background_corner_menu);
		
		listView.setAdapter(mMenuListAdapter);
		
		if (this.mOnItemClickListener != null)
			listView.setOnItemClickListener(mOnItemClickListener);
		else
			listView.setOnItemClickListener(new onItemClickListener());
	}
	
	public void setListItem(List<ImageItem> itemList){
		items.clear();
		for(ImageItem item : itemList){
			items.add(item);
		}
		
		mMenuListAdapter.notifyDataSetChanged();
	}
	
	class onItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Item mItem = items.get(position);
			String str=items.get(position).getGnlj();
			String key = mItem.getKey();
			
			if(str!=null && str.equals("hddjxx")){
				Intent intent = new Intent();
				intent.putExtra("path","bank/hdtz/hdtz!add.do?hdid="+key);
	    		intent.putExtra("name", items.get(position).getValue());
	    		intent.setClassName(context, "com.gdth.bank.activity.WebViewActivity");
	    		context.startActivity(intent);
			}else{
				joinToActivity(key);
			}
			
		}
	}
	
	private void joinToActivity(String val){
    	Intent intent = new Intent();
    	intent.setClassName(this.getContext(), val);
    	
		context.startActivity(intent);
		
		this.dismiss();
    }
}
